/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services.Impl;

import java.util.Collection;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import models.ProductType;
import models.Products;
import services.interfaces.IProductService;

/**
 *
 * @author SERGA
 */

@Stateless
@LocalBean
public class ProductsServiceImpl extends GenericServiceImpl<Products> implements IProductService{

     @PersistenceContext(unitName="orsPU")
    private EntityManager em;
    
    
    public ProductsServiceImpl() {
        super(Products.class);
    }
    
    @Override
    protected EntityManager getEntityManager() {
       return this.em;
    }

    @Override
    public Collection<Products> getProductByType(ProductType prType) {
        Query q = em.createNamedQuery("Products.findByPrType").setParameter("prType", prType);
        Collection entities = q.getResultList();
        return  entities;
    }

    @Override
    public Collection<Products> getProductByPrice(double price) {
        Query q = em.createNamedQuery("Products.findByPrice").setParameter("price", price);
        Collection entities = q.getResultList();
        return entities;
    }

    @Override
    public Products getProductByUnit(String unit) {
        Query q = em.createNamedQuery("Products.findByUnit").setParameter("unit", unit);
        Products p = (Products)q.getSingleResult();
        return p;
    }
   
}
